<template>
  <!-- 借进入库 -->
  <div>
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="part_report_adujustIn"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectHandle"
      selectionType="single"
      :isExportExcel="true"
      :onCalcExportData="exportFun"
      exportFileName="配件综合情况查询-借进入库.xlsx"
      :dataSource="dataSource"
      :isMemoryPagination="true"
    >
      <!-- :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"-->
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :disabled="printFlag" :click="printHandle">打印</AjaxButton>
      </template>
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :click="exportHandle">导出</AjaxButton>
      </template>
    </FilterTable>
    <div v-if="printBtn">
      <BasePrint ref="Salesprint" :data="SalesprintList" template="templatepartInventory" />
      <!-- 原templatePartAdjustInventory -->
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartInOutStockReport, getAllUser ,exportPartInOutStockReport } from '@/api/part/report/billSearch';
import { printData } from '@/api/part/partInventory/adjustInventory/index';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { dealerPrintApi } from '@/api/repair/index.js';
import { exportExcelFile } from '@/utils/index';

export default {
  mixins: [dictionary],
  props: {
    fetchParamObj: Object,
    type: String,
    userList: Array,
    tax: Number,
    userItems: Array
  },

  data() {
    return {
      fetchApi: queryPartInOutStockReport,
      columns: this.createdTable(),
      fetchParams: { xhrAbort: true },
      selectColumns: [],
      printFlag: false, // 打印按钮
      SalesprintList: {}, // 打印的数据
      printBtn: false, // 控制打印
      printParams: {},
      cunUserList: [], // 存所有人的数组
      printDataList: [], // 存打印接口返回的数据
      dataSource: []
    };
  },
  watch: {
    userList(val) {
      for (const key in val) {
        arr[{ text: val[key], value: key }];
      }
      console.log(123, arr);
      this.columns.find(item => item.dataIndex == 'OPERATOR_NAME').filterItems = arr;
    },
    fetchParamObj(fetchParamObj) {
      // console.log(fetchParamObj, 'fetchParamObj----1');
      this.initTableData(fetchParamObj);
    }
  },
  mounted() {
    // this.getStorage();
    this.getEmployee();
    // this.getPrintParams();
  },
  methods: {
    exportFun(row) {
      for (const key in row) {
        switch (key) {
          case 'OPERATOR_NAME':
            console.log(row[key]);
            row[key] = this.userList[row[key]];
            console.log(row[key]);
            break;
          // 还出操作人
          case 'HANDLER':
            if (row[key]) {
              row[key] = this.userList[row[key]];
            }
            break;
          case 'IN_QUANTITY':
          case 'OUT_QUANTITY':
          case 'IN_AMOUNT':
          case 'OUT_AMOUNT':
            // case 'COST_AMOUNT':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          default:
            break;
        }
      }
    },
    // 选中回调
    selectHandle(val) {
      this.selectColumns = val;
    },
    // 打印数据接口
    async getPrintData() {
      // console.log(this.selectColumns[0].PART_ALLOCATE_IN_ID);
      const res = await printData(this.selectColumns[0].PART_ALLOCATE_IN_ID); // 获取当前行数据
      res.data.detailInfo.map(item => {
        item.IN_AMOUNT_TAXED = item.IN_AMOUNT * (1 + this.tax * 1) || 0;
        item.IN_PRICE_TAXED = item.IN_PRICE * (1 + this.tax * 1) || 0;
      });
      this.printDataList = res.data;
    },

    async getPrintParams() {
      let params = { vModule: '90211009', vNormalCode: 'BJJR' };
      const res = await dealerPrintApi(params);
      if ((res.resultCode == 200) & res.data.length) {
        this.printParams = res.data[0];
      }

      console.log('打印的参数', this.printParams);
    },
    //导出
    async exportHandle(){
      let res = await exportPartInOutStockReport(this.fetchParams);
      console.log(res,"导出::");
      try{
        exportExcelFile(res.data,'借进入库')
      }catch(e){
        console.log(e,"错误::");
      }
    },
    // 打印处理
    async printHandle() {
      // this.getPrintParams();
      // 判断是否选中数据
      if (this.selectColumns.length < 1) {
        return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
      }
      await this.getPrintData();
      this.printBtn = true;

      this.SalesprintList = this.printDataList;
      this.SalesprintList.baseInfo.STOCK_IN_TYPE = '借进入库';
      // console.log(this.cunUserList, this.SalesprintList, this.printDataList);
      this.SalesprintList.baseInfo.USER_NAME = this.cunUserList[this.SalesprintList.baseInfo.USER_ID];
      // 打印
      this.$nextTick(() => {
        this.$refs.Salesprint.EXCUTE_PRINT();
      });
    },

    async initTableData(obj) {
      const fetchParamObj = this.fetchParamObj;
      // this.fetchParams = Object.assign({}, fetchParamObj, { xhrAbort: false });
      this.fetchParams = Object.assign({}, fetchParamObj);
      try {
        this.$refs.table.START_LOADING();
        this.dataSource = [];
        let res = await queryPartInOutStockReport(this.fetchParams);
        this.dataSource = res.data;
        this.$refs.table.STOP_LOADING();
      } catch (error) {
        console.log(error);
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },

    // 获取仓库
    async getStorage() {
      const res = await getStroageList();
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE + '' }));
    },
    // 操作员名称
    async getEmployee() {
      // 获取人员列表
      const res = await getAllUser();
      if (res.resultCode == 200) {
        res.data.map(e => {
          this.cunUserList[e.userId + ''] = e.employeeName;
        });
      }
      // this.columns.find(item => item.dataIndex === 'HANDLER').filterItems = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '入出库单编号',
          dataIndex: 'ALLOCATE_IN_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '对应单编号',
          dataIndex: 'ALLOCATE_OUT_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '对方单位名称',
        //   dataIndex: 'CUSTOMER_CODE',
        //   sorter: true
        // },
        {
          title: '对方单位名称',
          dataIndex: 'CUSTOMER_NAME',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '仓库代码',
          dataIndex: 'STORAGE_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          width: '160',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '批号',
        //   dataIndex: 'fake2',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '配件说明',
          dataIndex: 'PART_DESCRIPTION',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: 'CCC件类型',
          dataIndex: 'IS_CCC_PART',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '保质期',
        //   dataIndex: 'SHELF_LIFT_MARK'
        // },
        {
          title: '入出库日期',
          dataIndex: 'FINISHED_DATE',
          sorter: true,
          width: '160',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          showOverflowTooltip: true,
          exportFormat: {
            t: 'd'
          }
        },
        { title: ' 操作员代码', dataIndex: 'OPERETOR', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        {
          title: ' 操作员名称',
          dataIndex: 'OPERATOR_NAME',
          render: props => {
            return <span>{this.userList[props.row.OPERATOR_NAME]}</span>;
          },
          dictItems: [],
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.userItems,
          showOverflowTooltip: true
        },
        {
          title: 'ABC分类',
          dataIndex: 'ABC_TYPE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8092'),
          dictItems: this.createDictList('8092'),
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.createDictText(props.row.ABC_TYPE, '8092')}</span>;
          }
        },
        {
          title: '计量单位',
          dataIndex: 'UNIT_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '借入数量',
          dataIndex: 'IN_QUANTITY',
          sorter: true,
          showOverflowTooltip: true,
          summation: true,
          filter: true,
          filterType: 'number',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '还出数量',
          dataIndex: 'OUT_QUANTITY',
          sorter: true,
          summation: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'number',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '不含税借入成本金额',
          dataIndex: 'IN_AMOUNT',
          summation: true,
          precision: 2,
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'number',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '不含税还出成本金额',
          dataIndex: 'OUT_AMOUNT',
          sorter: true,
          showOverflowTooltip: true,
          precision: 2,
          summation: true,
          filter: true,
          filterType: 'number',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '还出出库单号',
          dataIndex: 'OUT_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '还出出库时间',
          dataIndex: 'FIN_DATA',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          showOverflowTooltip: true,
          exportFormat: {
            t: 'd'
          }
        },
        {
          title: '还出操作人',
          dataIndex: 'HANDLER',
          dictItems: [],
          render: props => {
            return <span>{this.userList[props.row.HANDLER]}</span>;
          },
          dictItems: [],
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        }
        // {
        //   title: '税前合计',
        //   dataIndex: 'IN_AMOUNT',
        //   sorter: true
        // },

        // {
        //   title: '备注',
        //   dataIndex: 'REMARK',
        //   sorter: true
        // }
      ];
    }
  }
};
</script>

<style lang="less" scoped>
/* .form-group {
  margin-top: 10px;
  span {
    height: 50px;
    line-height: 50px;
    text-align: right;
    width: 250px;
    padding-left: 100px;
  }
} */
</style>
